package com.mdlib.helper.ui.fragments.web;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.core.util.LogUtil;
import com.ljlib.help.R;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.entity.WebViewEntity;
import com.mdlib.helper.utils.webhelper.WVJBWebViewClient;
import com.orhanobut.logger.Logger;

import butterknife.Bind;

/**
 * 作者：HQY on 16/12/1 14:47
 * 邮箱：hqy_xz@126.com
 */

public class WebViewFragment extends BaseTitleFragment {

    private static String WEBVIEW = "webview";
    @Bind(R.id.webView)
    WebView mWvGeneric;
    private WebViewEntity mWebViewInfo;
    private WVJBWebViewClient mWebViewClient;


    public static WebViewFragment newInstance(WebViewEntity webViewEntity) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(WEBVIEW, webViewEntity);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mWebViewInfo = (WebViewEntity) bundle.getSerializable(WEBVIEW);
        }
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_webview;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(mWebViewInfo.getTitle(), R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });

        init();
    }

    public void init() {
        WebSettings webSettings = mWvGeneric.getSettings();
        webSettings.setJavaScriptEnabled(true); // 启用支持javascript
        webSettings.setAllowFileAccess(true); // 可以访问文件
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
        String ua = webSettings.getUserAgentString();
        webSettings.setUserAgentString(ua + "(MDAPP)");
        mWvGeneric.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWvGeneric.loadUrl(mWebViewInfo.getUrl() + "?session_id=" + AccountModel.getInstance().getSessionId());
        mWebViewClient = new MyWebViewClient(mWvGeneric);
        mWvGeneric.setWebViewClient(mWebViewClient);
    }
    private class MyWebViewClient extends WVJBWebViewClient {
        public MyWebViewClient(WebView webView) {

            super(webView, new WVJBHandler() {
                @Override
                public void request(Object data, WVJBResponseCallback callback) {
                }
            });
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Logger.d("url: " + url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Logger.e(mWvGeneric.getSettings().getUserAgentString());
            LogUtil.e("标题：" + view.getTitle());
            if (EmptyUtils.isNotEmpty(view.getTitle())) {
                setTitleText(view.getTitle());
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description,
                                    String failingUrl) {
//            onWebLoadError();
        }
    }
}
